import {   PAY_WAY, PAY_WAY_LIST} from "@xiaoe/etransaction";
/**
 * 生成对应支付方式所需要的参数对象
 * @param {string} type 支付类型
 * @param {object} payResult 下单接口返回对象
 * @param {object} jumpData 支付后的重定向链接对象
 * @param {string} goodsName 商品名称
 * @param {string} ratePrice 商品价格
 * @param {string} spuId 商品spuid
 * @param {string} isTeam 标志是否是拼团
 * @returns payInfoObj 请求支付入参对象
 */
export function producePayParams(
  type,
  payResult,
  jumpData,
  goodsName,
  ratePrice,
  spuId,
  isTeam
) {
  const { payFailUrl, paySuccessUrl } = jumpData;
  const { pay_way,pay_config} = payResult?.pay_config;
  // alert(JSON.stringify(payResult))
  const payConfig = encodeURIComponent(JSON.stringify(pay_config));
  const appId = payResult?.app_id;
  let payInfoObj = {},
    payUrl = "",
    env = "";
  switch (type) {
    //微信内api支付
    case PAY_WAY.WxJSAPIPay:
      return payResult?.pay_config?.payConfig;
    //跳链接微信app支付
    case PAY_WAY.WxH5Pay:
      payUrl = payResult?.pay_config?.mweb_url;
      if (typeof payUrl === "string") {
        payUrl = payUrl.replace(/\\/g, "");
      }
      return {
        payResult,
        payUrl,
        payFailUrl: encodeURIComponent(payFailUrl) || "", //支付失败跳转的连接
        paySuccessUrl: encodeURIComponent(paySuccessUrl) || "", //支付成功跳转的连接
      };
    //支付宝支付
    case PAY_WAY.AliPay:
    case PAY_WAY.AliHuabeiPay:
      payUrl = payResult?.pay_config?.mweb_url;
      if (typeof payUrl === "string") {
        payUrl = payUrl.replace(/\\/g, "");
      }
      return {
        payResult,
        tradeId: payResult.trade_id,
        origin: window.location.origin,
        payUrl: encodeURIComponent(payUrl || ""),
        orderId: payResult.order_id || "",
        srcUrl: encodeURIComponent(payFailUrl || ""), //支付失败跳转的连接
        backUrl: encodeURIComponent(paySuccessUrl || ""), //支付成功跳转的连接
      };
    //吱口令支付
    case PAY_WAY.AliPayCommand:
      return {};
    //象币虚拟币支付
    case PAY_WAY.VirtualPay:
      return {
        ...this.payResult, //下单信息
        goods_name: goodsName, //商品名称
        price: ratePrice, //金额
      };
    //快手小程序支付
    case PAY_WAY.KSMiniProgramPay:
      const {order_info_token, order_no} = payResult?.pay_config?.ks_pay_config?.order_info || {}
      return {
        payInfo: {
          order_info_token,
          order_no,
          back_url: paySuccessUrl || "",
          src_url: payFailUrl || window.location.href,
          app_id: window.APPID,
        },
        spu_id: spuId,
        isTeam: isTeam ? isTeam : "",
      };
    //店铺小程序、鹅圈子小程序、独立圈子小程序、视频号小程序、视频号分销小程序支付
    case PAY_WAY.MpBlankPay:
      return {
        order_id: payResult.order_id || "",
        success_back_url: paySuccessUrl || "",
        failed_back_url: payFailUrl || window.location.href,
        back_url: paySuccessUrl || "",
        src_url: payFailUrl || window.location.href,
        app_id: window.APPID,
      };
    //非服务商模式、购物车下单支付跳空白页
    case PAY_WAY.H5BlankPay:
      // 判断环境
      if (document.URL.indexOf(".inside.") > -1) {
        env = ".inside";
      } else if (document.URL.indexOf(".test.") > -1) {
        env = ".test";
      }
      // 自有
      if (pay_way == PAY_WAY_LIST.WEIXIN_PAYMENT) {
        payUrl = `https://${appId}.h5${env}.xiaoeknow.com/content_page/newPayCode`;
      } else {
        // 购物车合单
        payUrl = `https://h5${env}.xiaoeknow.com/content_page/newPayCode`;
      }
      if((pay_way == PAY_WAY_LIST.WEIXIN_PAYMENT)){
        return {
          payUrl,
          payConfig,
          failed_back_url: encodeURIComponent(
            window.location.origin + payFailUrl
          ), //支付失败跳转的连接
          success_back_url: encodeURIComponent(
            window.location.origin + paySuccessUrl
          ), //支付成功跳转的连接
          app_id: window.APPID || "",
        };
      }
      return {
        payUrl,
        payConfig,
        failed_back_url: encodeURIComponent(
           payFailUrl
        ), //支付失败跳转的连接
        success_back_url: encodeURIComponent(
           paySuccessUrl
        ), //支付成功跳转的连接
        app_id: window.APPID || "",
      };
    case PAY_WAY.PayPal:
      payUrl = payResult?.pay_config?.mweb_url;
      if (typeof payUrl === "string") {
        payUrl = payUrl.replace(/\\/g, "");
      }
      return {
        payResult,
        payUrl,
        payFailUrl: encodeURIComponent(payFailUrl) || "", //支付失败跳转的连接
        paySuccessUrl: encodeURIComponent(paySuccessUrl) || "", //支付成功跳转的连接
      };
    default:
      return payInfoObj.payConfig;
  }
}
